import { expect } from '@jest/globals';
import * as compressing from 'compressing';
import fsExtra from 'fs-extra';
import path from 'path';

import { initExecuteCommand, runHvigorAsync } from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  IsEnable,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
  SignType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOption } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.NativeC
  },
  modulesOption: {
    [ModuleTemplateType.NativeC]: 1,
  }
};

const taskConfig: TestTaskOption = {
  caseLevel: CaseLevel.L1,
  sign: SignType.NoSign,
  ohpmInstall: IsEnable.Enable,
};
const testFun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRootPath = project.projectRootPath();
  const commands = initExecuteCommand({
    mode: 'module',
    product: 'default',
    tasks: 'assembleHap',
  });
  const result = await runHvigorAsync(projectRootPath, commands);
  record.addStep('执行hap编译构建', result);
  expect(result.stdout).toContain('Finished :entry:default@PackageHap');
  expect(result.stdout).toContain('Finished :nativeApplication:default@PackageHap');
  expect(result.stdout).toContain('BUILD SUCCESSFUL');

  // 解析entry模块下的hap包
  const modules = ['entry', 'nativeApplication'];
  for (let i = 0; i < modules.length; i++) {
    const moduleName = modules[i];
    const modulePath = project.getModule(moduleName).moduleRootPath();
    if (moduleName === 'nativeApplication') {
      await fsExtra.copyFile(
        path.resolve(modulePath, 'build/default/outputs/default/', `${moduleName}-entry-default-unsigned.hap`),
        path.resolve(modulePath, 'build/default/outputs/default/', `${moduleName}-default-unsigned.zip`));
    } else {
      await fsExtra.copyFile(
        path.resolve(modulePath, 'build/default/outputs/default/', `${moduleName}-default-unsigned.hap`),
        path.resolve(modulePath, 'build/default/outputs/default/', `${moduleName}-default-unsigned.zip`));
    }
    await compressing.zip.uncompress(
      path.resolve(modulePath, 'build/default/outputs/default/', `${moduleName}-default-unsigned.zip`),
      path.resolve(modulePath, 'build/default/outputs/default/', `${moduleName}-default-unsigned`));

    // 判断是否含有 libentry.so 文件
    const exists1 = await fsExtra.pathExists(
      // eslint-disable-next-line max-len
      path.resolve(modulePath, 'build/default/outputs/default/', `${moduleName}-default-unsigned/libs/arm64-v8a/lib${moduleName}.so`));

    // 判断是否含有 libc++_shared.so 文件
    const exists2 = await fsExtra.pathExists(
      // eslint-disable-next-line max-len
      path.resolve(modulePath, 'build/default/outputs/default/', `${moduleName}-default-unsigned/libs/arm64-v8a/libc++_shared.so`));

    // 判断是否含有 compile_commands.json 文件
    const exists3 = await fsExtra.pathExists(
      // eslint-disable-next-line max-len
      path.resolve(modulePath, '.cxx/default/default/debug/arm64-v8a/compile_commands.json'));
    expect(exists1 && exists2 && exists3).toBe(true);
  }
};

TestCaseBuilder.builder()
  .caseName('OPEN_LAB_ZOS_Build_Stage_Cpp_eTS_HOS_api10_STAGE_001')
  .description('Stage模型eTS+cpp支持cpp编译任务')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(taskConfig)
  .testFun(testFun)
  .load();
